package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

func main()  {
	//单个进程中制造复制请求，在10个处理程序上复制模拟请求的示例：
	doWork := func(
		done <-chan interface{},
		id int,
		wg *sync.WaitGroup,
		result chan<- int,
	) {
		started := time.Now()
		defer wg.Done()

		//模拟随机负载
		simulatedLoadTime := time.Duration(1+rand.Intn(5))*time.Second
		select {
		case <-done:
		case <-time.After(simulatedLoadTime):
		}

		select {
		case <-done:
		case result <- id:
		}

		took := time.Since(started)
		//显示处理程序需要多长时间
		if took < simulatedLoadTime {
			took = simulatedLoadTime
		}
		fmt.Printf("%v took %v\n",id,took)
	}

	done := make(chan interface{})
	result := make(chan int)

	var wg sync.WaitGroup
	wg.Add(10)

	for i := 0;i < 10;i++ {    //启动10个处理程序来处理请求
		go doWork(done,i,&wg,result)
	}

	firestReturned := <-result //获得处理程序组的第一个返回值
	close(done)    //取消其余的处理程序，保证他们不会继续做多余的工作
	wg.Wait()

	fmt.Printf("Received an answer from #%v\n",firestReturned)


}


